﻿package com.bizmlib.utils.sets
{
	
	public class SetManagerFlatOrderedList extends SetManager
	{
		
		
		public function SetManagerFlatOrderedList(nodeName:String = undefined, keyAttribute:String = undefined)
		{
			itemNodeName = nodeName || itemNodeName;
			this.keyAttribute = keyAttribute || this.keyAttribute;
		}
		
		
		/**
		override public function parseXML(newXMLNode:XMLNode, oldXMLNode:XMLNode):void
		{
			//trace(this + ".parseXML(" + arguments + ")");
			
			var oldItemsList:Array = siftOutNodes(oldXMLNode);
			var newItemsList:Array = siftOutNodes(newXMLNode);
			var newItemXML:XML;
			var oldItemXML:XML;
			var additionItemXML:XML;
			
			
			var additionItemsList:Array = newItemsList.concat();
			//trace("additionItemsList=" + additionItemsList);
			for(var i:Number=0; i<oldItemsList.length; i++)
			{
				oldItemXML = oldItemsList[i] as XML;
				var isExist:Boolean = false;
				
				for(var j:Number=0; j<additionItemsList.length; j++)
				{
					additionItemXML = additionItemsList[j] as XML;
					
					if(oldItemXML.attribute(keyAttribute)==additionItemXML.attribute(keyAttribute))
					{
						isExist=true;
						dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_UPDATE, j, i, additionItemXML));
						additionItemsList.splice(j, 1);
						break;
					}
				}
				if(!isExist)
				{
					oldItemsList = (oldItemsList.slice(0, i)).concat(oldItemsList.slice(i+1));
					dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_REMOVE, i, undefined, oldItemXML));
					i--;
				}
			}
			
			for(var i:Number=0; i<newItemsList.length; i++)
			{
				newItemXML = newItemsList[i] as XML;
				var isNew:Boolean = false;
				
				for(var j:Number=0; j<additionItemsList.length; j++)
				{
					additionItemXML = additionItemsList[j] as XML;
					
					if(newItemXML.attribute(keyAttribute)==additionItemXML.attribute(keyAttribute))
					{
						dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_ADD, undefined, i, additionItemXML));
						isNew = true;
						break;
					}
				}
				if(!isNew)
				{
					for(var j:Number=0; j<oldItemsList.length; j++)
					{
						var oldItemXML:XML = oldItemsList[j];
						
						if(newItemXML.attribute(keyAttribute)==oldItemXML.attribute(keyAttribute))
						{
							if(j!=i)
							{
								var localBuffer:XML = oldItemsList[i];
								oldItemsList[i] = oldItemXML;
								oldItemsList[j] = localBuffer;
								dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_MOVE, j, i, newItemXML));
							}
							break;
						}
					}
				}
			}
		}
		/**/
		
		override public function parseArray(newItemsList:Array, oldItemsList:Array):void
		{
			//trace(this + ".parseArray(" + arguments + ")");
			
			var i:Number;
			var j:Number;
			
			newItemsList = newItemsList || new Array();
			oldItemsList = oldItemsList || new Array();
			var additionItemsList:Array = newItemsList.concat();
			
			var oldItemData:Object;
			var newItemData:Object;
			var additionItemData:Object;
			
			
			for(i=0; i<oldItemsList.length; i++)
			{
				oldItemData = oldItemsList[i];
				var isExist:Boolean = false;
				
				for(j=0; j<additionItemsList.length; j++)
				{
					additionItemData = additionItemsList[j];
					
					if(oldItemData[keyAttribute]==additionItemData[keyAttribute])
					{
						isExist=true;
						dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_UPDATE, i, undefined, additionItemData));
						additionItemsList.splice(j,1);
						break;
					}
				}
				
				if(!isExist)
				{
					oldItemsList = (oldItemsList.slice(0, i)).concat(oldItemsList.slice(i+1));
					dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_REMOVE, i, undefined, oldItemData));
					i--;
				}
			}
	
			for(i=0; i<newItemsList.length; i++)
			{
				newItemData = newItemsList[i];
				var isNew:Boolean = false;
	
				for(j=0; j<additionItemsList.length; j++)
				{
					additionItemData = additionItemsList[j];
					
					if(newItemData[keyAttribute]==additionItemData[keyAttribute])
					{
						dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_ADD, undefined, i, additionItemData));
						oldItemsList[i] = additionItemData;
						isNew = true;
						break;
					}
	
					//trace("isNew=" + isNew);
				}
				if(!isNew){
					for(j=0; j<oldItemsList.length; j++)
					{
						oldItemData = oldItemsList[j];
						if(newItemData[keyAttribute]==oldItemData[keyAttribute])
						{
							if(j!=i)
							{
								var buffer:Object = oldItemsList[i];
								oldItemsList[i] = oldItemsList[j];
								oldItemsList[j] = buffer;
								
								dispatchEvent(new SetManagerEvent(SetManagerEvent.ITEM_MOVE, j, i, newItemData));
							}
							break;
						}
					}
				}
			}
		}
	
	}
}